package xyz.eden.gateway.shenyu.plugin.sign;

import lombok.extern.slf4j.Slf4j;
import org.apache.shenyu.common.dto.RuleData;
import org.apache.shenyu.common.dto.SelectorData;
import org.apache.shenyu.common.utils.JsonUtils;
import org.apache.shenyu.plugin.api.ShenyuPluginChain;
import org.apache.shenyu.plugin.api.result.ShenyuResultWrap;
import org.apache.shenyu.plugin.api.utils.WebFluxResultUtils;
import org.apache.shenyu.plugin.base.AbstractShenyuPlugin;
import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import xyz.eden.gateway.shenyu.plugin.sign.constants.PluginInfo;
import xyz.eden.gateway.shenyu.plugin.sign.constants.Properties;
import xyz.eden.gateway.shenyu.plugin.sign.constants.ResultInfo;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

@Slf4j
public class ApiSignPlugin extends AbstractShenyuPlugin {
    @Override
    protected Mono<Void> doExecute(ServerWebExchange exchange, ShenyuPluginChain chain, SelectorData selector, RuleData rule) {

        ApiSignProperties properties = this.getApiSignProperties(exchange);

        boolean checkResult = checkApiSign(properties);
        if (log.isInfoEnabled()) {
            log.info("[ApiSignPlugin] checkApiSign - properties: [{}], checkResult: [{}].", JsonUtils.toJson(properties), checkResult);
        }
        if (!checkResult) {
            Object error = ShenyuResultWrap.error(exchange, ResultInfo.FAIL_CODE, ResultInfo.FAIL_MESSAGE, null);
            return WebFluxResultUtils.result(exchange, error);
        }
        return chain.execute(exchange);
    }

    private boolean checkApiSign(ApiSignProperties properties) {
        if (Objects.isNull(properties)) {
            return false;
        }

        String appKey = properties.getAppKey();
        MultiValueMap<String, String> paramMap = properties.getParamMap();
        String requestId = properties.getRequestId();
        ZonedDateTime timestamp = properties.getTimestamp();
        log.info("[ApiSignPlugin] checkApiSign: appKey: [{}], timestamp: [{}].", appKey, timestamp);

        return true;
    }

    private ApiSignProperties getApiSignProperties(ServerWebExchange exchange) {
        // Header 数据
        HttpHeaders httpHeaders = exchange.getRequest().getHeaders();
        String appKey = httpHeaders.getFirst(Properties.APP_KEY);
        String first = httpHeaders.getFirst(Properties.TIMESTAMP);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        ZonedDateTime timestamp = ZonedDateTime.parse(first, dateTimeFormatter);
        String requestId = httpHeaders.getFirst(Properties.REQUEST_ID);

        // Param
        MultiValueMap<String, String> multiValueMap = exchange.getRequest().getQueryParams();
        ApiSignProperties properties = new ApiSignProperties();
        properties.setAppKey(appKey);
        properties.setTimestamp(timestamp);
        properties.setRequestId(requestId);
        properties.setParamMap(multiValueMap);

        return properties;
    }

    @Override
    public int getOrder() {
        return PluginInfo.ORDER;
    }

    @Override
    public String named() {
        return PluginInfo.NAMED;
    }
}
